/******************************************************************************
 * Copyright (C) 2024 EternalChip, Inc.(Gmbh) or its affiliates.
 * 
 * All Rights Reserved.
 * 
 * @file key.c
 * 
 * @par dependencies 
 * - bsp_key.h
 * - stdio.h
 * - stdint.h
 * 
 * @author Jack | R&D Dept. | EternalChip ��оǶ��ʽ
 * 
 * @brief Provide the HAL APIs of Key and corresponding opetions.
 * 
 * Processing flow:
 * 
 * call directly.
 * 
 * @version V1.0 2024-10-30
 *
 * @note 1 tab == 4 spaces!
 * 
 *****************************************************************************/

#include "bsp_key.h"
osThreadId_t key_TaskHandle;

QueueHandle_t key_queue;

const osThreadAttr_t key_Task_attributes = {
  .name = "key_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityHigh,
};



key_status_t key_scan(key_press_status_t * key_value)
{
    
    uint32_t counter = 0;
    key_press_status_t key_status_value = KEY_NOT_PRESSED;
    
    while (counter < 1000){
        
        if(HAL_GPIO_ReadPin(Key_GPIO_Port, Key_Pin) == GPIO_PIN_RESET)
        {
            key_status_value = KEY_PRESSED;
            *key_value = key_status_value;
            return KEY_OK;
        }
        //2.2 �������û�б�����
        counter++;
    }
    *key_value = key_status_value;
    
    return KEY_ERRORTIMEOUT;//ʼ��û�а��������£����س�ʱ
}

key_status_t key_scan_short_long_press(key_press_status_t * key_value,
                                       uint32_t short_press_time)
{
    /**Variables** */
    key_status_t ret_key_status       = KEY_OK;
    key_press_status_t key_value_temp = KEY_NOT_PRESSED;
    uint32_t count_tick               = 0;
    /**Variables** */

    //1.check if the key is pressed
    ret_key_status = key_scan(&key_value_temp);
    //1.1 if the key is pressed,then check if it is short pressed
    if(KEY_OK == ret_key_status)
    {
        if(key_value_temp == KEY_PRESSED)
        {
            count_tick = HAL_GetTick();
            //1.1.1 check the short press time 
            while(HAL_GetTick() < count_tick + short_press_time)
            ;
            ret_key_status = key_scan(&key_value_temp);
						
            if(KEY_NOT_PRESSED == key_value_temp)
            {
                * key_value = KEY_SHORT_PRESSED;              
                return KEY_OK;
            }
            else
            {
                * key_value = KEY_LONG_PRESSED;
							
							//1.1.2.1keep focuing on the status of key to avoid the short
							while(KEY_OK == key_scan(&key_value_temp))
								;
                 return KEY_OK;
            }
            
        }
    }
    return ret_key_status;
}

void key_task_func(void *argument)
{
    
  key_status_t  ket_ret          =          KEY_OK;
  key_press_status_t key_statues = KEY_NOT_PRESSED;
  key_queue = xQueueCreate( 10, sizeof( uint32_t ) );
  uint32_t counter_tick = 0;
  if (NULL == key_queue )
  {
      printf("key_queue created failed \r\n");
  } 
  else
  {
      printf("key_queue created successfully \r\n");
  }
  for(;;)
  {
    counter_tick++;
    //printf("Hellow Key thread\r\n");
    
    ket_ret = key_scan(&key_statues);
    
    if( KEY_OK == ket_ret)
    {
        if ( KEY_PRESSED == key_statues )
        {
            printf("Key_Pressed\r\n");
            if ( pdTRUE == xQueueSendToFront(key_queue,&counter_tick,0))
            {
                printf("send successfully\r\n");
            }
        }
    }
    if( KEY_OK != ket_ret)
    {
        printf("Key_not_Pressed\r\n");
    }        
    osDelay(100);
  }
}
  /* USER CODE END 5 */























